
const todosController = function() {
  'ngInject';

  const vm = this;

  vm.todos = [
    { id: 1, title: '完成系统文档整理', completed: false, priority: 'high', dueDate: '2024-01-16' },
    { id: 2, title: '处理用户反馈问题', completed: true, priority: 'medium', dueDate: '2024-01-15' },
    { id: 3, title: '代码审查和测试', completed: false, priority: 'high', dueDate: '2024-01-17' },
    { id: 4, title: '部署生产环境', completed: false, priority: 'low', dueDate: '2024-01-20' }
  ];

  vm.newTodo = {
    title: '',
    priority: 'medium',
    dueDate: ''
  };

  vm.addTodo = function() {
    if (vm.newTodo.title) {
      const todo = {
        id: Date.now(),
        title: vm.newTodo.title,
        completed: false,
        priority: vm.newTodo.priority,
        dueDate: vm.newTodo.dueDate
      };
      vm.todos.unshift(todo);
      vm.newTodo = { title: '', priority: 'medium', dueDate: '' };
    }
  };

  vm.toggleTodo = function(todo) {
    todo.completed = !todo.completed;
  };

  vm.deleteTodo = function(id) {
    vm.todos = vm.todos.filter(todo => todo.id !== id);
  };

  vm.getPriorityClass = function(priority) {
    return {
      'high': 'badge-danger',
      'medium': 'badge-warning',
      'low': 'badge-secondary'
    }[priority];
  };

  vm.getCompletedCount = function() {
    return vm.todos.filter(todo => todo.completed).length;
  };

  vm.getPendingCount = function() {
    return vm.todos.filter(todo => !todo.completed).length;
  };
};

const todosComponent = {
  templateUrl: 'src/app/pages/todos/todos.template.html',
  controller: todosController
};

function initTodosController() {
  const angular = window.angular;
  
  if (!angular) {
    console.error('Angular 未找到，无法初始化任务控制器模块');
    return;
  }

  angular.module('app.pages.todos', [])
    .component('todosPage', todosComponent);
}

// Listen for vendor libs loaded event
window.addEventListener('vendorLibsLoaded', () => {
  initTodosController();
});

// If Angular already loaded, init directly  
if (window.angular) {
  initTodosController();
}